//
//  LoginViewController.m
//  Exam14-1
//
//  Created by 孙晓晔 on 2017/5/5.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
    [notify addObserver:self selector:@selector(onChange) name:UITextFieldTextDidChangeNotification object:self.username];
    [notify addObserver:self selector:@selector(onChange) name:UITextFieldTextDidChangeNotification object:self.password];
}

-(void)dealloc {
    NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
    [notify removeObserver:self name:UITextFieldTextDidChangeNotification object:self.username];
    [notify removeObserver:self name:UITextFieldTextDidChangeNotification object:self.password];
}

-(void)onChange {
    if (self.username.text.length > 0
        && self.password.text.length > 0) {
        self.loginBtn.enabled = TRUE;
    } else {
        self.loginBtn.enabled = FALSE;
    }
}

-(NSInteger) auth {
    // 1.设置请求路径
    NSString *strUrl = [NSString stringWithFormat:@"http://vpn.jiapuwang.net/j/one/tally.php?do=User.login&username=%@&password=%@",self.username.text,self.password.text];
    
    NSURL *url = [NSURL URLWithString:strUrl];
    
    // 2.创建请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 3.发送请求
    // 发送同步请求，在主线程执行
    // @deprecated
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    //    NSLog(@" length = %ld, data = %@", data.length, [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSLog(@"%@",dict);
    
    NSInteger code = [dict[@"code"] integerValue];

    return code;
}

- (IBAction)login:(id)sender {
    NSLog(@"登录", nil);
    
    NSInteger code = [self auth];
//    NSInteger code = 1;
    
    if (code == 1) {
        // 验证成功
        
        [self performSegueWithIdentifier:@"login2tally" sender:nil];
        
    } else {
        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录失败，请重新登录" preferredStyle: UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
